package com.quan.web.controller.weixin;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.annotation.RequiresPermissions;
import com.quan.common.base.pojo.JSONResult;
import com.quan.common.base.pojo.PageBean;
import com.quan.common.core.support.BaseController;
import com.quan.weixin.rpc.api.WeixinArticleGroupArticleService;
import com.quan.weixin.rpc.api.WeixinArticleGroupService;
import com.quan.weixin.rpc.model.WeixinArticleGroup;

@RestController
@RequestMapping(value = "/api/weixin/articleGroup")
public class WeixinArticleGroupController extends BaseController {
	
	@Autowired
	private WeixinArticleGroupService weixinArticleGroupService;

	@Autowired
	private WeixinArticleGroupArticleService weixinArticleGroupArticleService;

	@RequiresPermissions(value="weixin:article_group:add", description="添加微信图文组")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinArticleGroup entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinArticleGroupService.insertSelective(entity);

		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article_group:delete", description="删除微信图文组")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinArticleGroupService.deleteById(id);
		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article_group:update", description="修改微信图文组")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinArticleGroup entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinArticleGroupService.update(entity);

		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article_group:detail", description="查询微信图文组详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinArticleGroupService.selectOneById(Integer.valueOf(id.toString()));
	}
	
	@RequiresPermissions(value="weixin:article_group:list", description="查询微信图文组")
	@GetMapping(value = "/list")
	public Object list(WeixinArticleGroup entity) {
		return this.weixinArticleGroupService.selectPage(entity, new PageBean(request)) ;
	}
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions(value="weixin:article_group:addArticleAssing", description="图文组关联图文")
	@PostMapping(value = "/addArticleAssign")
	public Object addArticleAssign(@RequestBody Map<String, Object> params) {
		Integer groupId = (Integer) params.get("groupId");
		List<Integer> articleIds = (List<Integer>) params.get("articleIds");
		
		if(null != articleIds && !articleIds.isEmpty()) {
			if(articleIds.size() > 8) {
				return JSONResult.failure("图文数量最多只能添加8条") ;
			}
			//获取数据库中的图文数量
			int articleCount = this.weixinArticleGroupService.getArticleCount(groupId) ;
			if(articleCount >= 8) {
				return JSONResult.failure("图文数量最多只能添加8条") ;
			} else {
				int total = articleIds.size() + articleCount ;
				if(total > 8) {
					return JSONResult.failure("当前添加图文数量已超过8条，还能添加["+(8-articleCount)+"]条") ;
				}
			}
		}
		
		this.weixinArticleGroupService.insertArticleGroupArticle(groupId, articleIds);
		
		return JSONResult.ok();
	}
	
	/**
	 * 查询图文组的图文列表
	 * @param groupId
	 * @return
	 */
	@GetMapping(value = "/selectByGroupId/{groupId}")
	public Object selectByGroupId(@PathVariable Integer groupId) {
		return this.weixinArticleGroupArticleService.selectByGroupId(groupId) ;
	}
	
	/**
	 * 根据图文组关系表ID删除关联图文
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/deleteArticleGroupArticle/{id}")
	public Object deleteArticleGroupArticle(@PathVariable Integer id) {
		this.weixinArticleGroupArticleService.deleteById(id) ;
		return JSONResult.ok();
	}
}
